/*$(document).on('click','#menuActive',function(){
	var $this=$(this);
	//$( "<p>Test</p>" ).insertAfter($this);
	$this.append(' <img src="images/ajax-loader.gif" />');
	var title=$this.text();
	var parent=$this.parents().eq(2).find('a').first().text();
	$.ajax({
		url:$(location).attr('href'),
		type:'get',
		data:'menuActive='+parent+'&menuActive2='+title,
		success:function(){
			location.href=$this.attr('url');;
		}
	});
});*/
var menu;
var calendar = null;

function selected(cal, date) {
	cal.sel.value = date;
}
function closeHandler(cal) {
	cal.hide();
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		calendar.hide();
		calendar.parseDate(el.value);
	} else {
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;
		cal.setRange(1900, 2070);
		calendar.create();
		calendar.parseDate(el.value);
	}
	calendar.sel = el;
	calendar.showAtElement(el);
	
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}
function altTable(){
	$('table.tadmin tbody tr:even').addClass('row1');
}
function admin_cb(){
	$('input#cbcall').click(function(){
		if($(this).is(':checked')){
			$('input[type="checkbox"][name="cb[]"]').each(function(){
				$(this).attr('checked','checked');
			});
		}else{
			$('input[type="checkbox"][name="cb[]"]').each(function(){
				$(this).attr('checked','');
			});
		}
	});
}
function tableSortby(id,type){
	$.ajax({
		url:orderlink,
		type:'post',
		data:'id='+id+'&type='+type,
		success:function(){
			location.href=indexlink;
		}
	});
}
function tanggal_focus(){
$('.form-group.khusus > .input-group > input').focus();
}
function addInpDateBut(el){
	$('.form-group.khusus > .input-group > input').attr('data-provide', 'datepicker');
	el.after('<span class="input-group-addon mouse" onclick="tanggal_focus();"><i class="fa fa-calendar"></i></span>');
}
function admin_table(){
	var lh=$('table.tadmin th').length;
	if($('table.tadmin tbody tr').length==0){
		$('table.tadmin tbody').append('<tr><td colspan="'+lh+'">No Data Available</td></tr>');
	}
	$('table.tadmin tfoot td').each(function(){
		$(this).attr('colspan',lh);
	});
	$('table.tadmin th').each(function(){
		if ($(this).attr('sort')){
			if ( $(this).attr('order') ) {
				if ( $(this).attr('order')=='asc' ) {
					type='desc';
				} else {
					type='asc';
				}
			} else {
				type='asc';
			}
			$(this).wrapInner('<a href="javascript:tableSortby(\''+$(this).attr('sort')+'\',\''+type+'\')"></a>');
		}
	});
	altTable();
	$('#searchForm').submit(function(){
		if ( $(this).find('input[name="keyword"]').val().length==0 ) {
			alert('Please specify what you are looking for!');
			return false;
		}
		var data=$(this).serialize();
		$.ajax({
			url:searchlink,
			type:'post',
			data:data,
			success:function(){
				location.href=indexlink;
			}
		});
		return false;
	});
	$('#searchForm').find('button.clearsearch').click(function(){
		$.ajax({
			url:searchlink,
			type:'post',
			data:'clear=1',
			success:function(){
				location.href=indexlink;
			}
		});
		return false;
	});
	var searchForm=$('#searchForm').find('select[name="fieldname"]');
	$(searchForm).on('change', function(){
		var val=$(this).val();
		$(this).find('option').each(function(){
			if(val==$(this).attr('value')){
				if($(this).attr('rel')=='date'){
					addInpDateBut($('#searchForm').find('input#search_keyword'));
					var opt='<option value="equal">Equal to</option>';
					opt+='<option value="greater">Greater than</option>';
					opt+='<option value="less">Less than</option>';
					$('#searchForm').find('select#search_criteria').html(opt);
					$('#searchForm').find('input[name="mode"]').val('date');
				}else{
					if($('#searchForm').find('input[name="mode"]').val()!='text'){
						$('#searchForm').find('input[name="mode"]').val('text');
						if ($('#searchForm').find('span.input-group-addon')){
							$('.form-group.khusus > .input-group input.keyword').remove();
							$('<input type="text" id="search_keyword" name="keyword"  class="keyword form-control">').insertBefore('.form-group.khusus > .input-group >.input-group-btn')
							$('#searchForm').find('span.input-group-addon').remove();
						}	

						var opt='<option value="equal">Equal to</option>';
						opt+='<option value="contains">Contains</option>';
						$('#searchForm').find('select#search_criteria').html(opt);
					}
				}
			}
		});
	});
	if($('#searchForm').find('input[name="mode"]').val()=='date'){
		addInpDateBut($('#searchForm').find('input#search_keyword'));
	}
	$('table.inweb_form label.label').parent().addClass('label');
}
function admdelete(){
	var cb=[];
	$('input[type="checkbox"][name="cb[]"]').each(function(){
		if($(this).is(':checked')){
			cb[cb.length]='cb[]='+$(this).val();
		}
	});
	
	if ( cb.length==0 ) {
		alert('Please select row to delete first');
	}else{
		if ( confirm('Are you sure you want to delete?') ){
			$.ajax({
				url:deletelink,
				type:'post',
				dataType:'json',
				data:cb.join('&'),
				success:function(data){
					if (data.stat=='failed'){
						alert(data.msg);
					}else{
						location.href=data.msg;
					}
				}
			});
		}
	}
}
function admin_tree(){
	$('div.tree input[type="checkbox"]').each(function(){
		$(this).click(function(){
			if($(this).is(':checked')){
				var c=true;
			}else{
				var c=false;
			}
			$(this).next().find('input[type="checkbox"]').each(function(){
				if(c){
					$(this).attr('checked','checked');
				}else{
					$(this).attr('checked','');
				}
			});
		});
	});
}
function initSubmenuIcon(){
	$('li.admin-delete a').attr('title','Delete').css({'display':'block','width':'16px','height':'14px'}).html('<img border="0" src="'+baseurl+'/assets/images/action_delete.png" />');
	$('li.admin-add a').attr('title','Add').css({'display':'block','width':'16px','height':'14px'}).html('<img border="0" src="'+baseurl+'/assets/images/action_add.png" />');
	$('li.admin-browse a').attr('title','Browse').css({'display':'block','width':'16px','height':'14px'}).html('<img border="0" src="'+baseurl+'/assets/images/application.png" />');
}
function upload(i){
var $type_file=$('input[type="file"]#files'+i);
var $type_file_name=$('input#filesname'+i);
//alert($type_file.html());
$type_file.click();
$type_file.change(function(){
$type_file_name.val($(this).val());
}); 
}
$(document).ready(function(){
	$('input.datetime').datetimepicker();
	/*	$('div.themes').each(function(k,i){
			if((k+1)%4==0){
				$(this).after('<div style="clear:both"></div>');
			}
		});*/
		$('div.themes').mouseover(function(){
			$('div.themes').removeClass('over');
			if(!$(this).hasClass('active')){
				$(this).addClass('over');
			}
		}).mouseout(function(){
			$(this).removeClass('over');
		});
		$('a.del').click(function(){
			if ( confirm("Are you sure you want to delete this theme?") )
			{
				location.href=$(this).attr('href');
			}
			return false;
		});
	});
$(document).ready(function(){
	
	var $panel=$('.tempat-table');
	var $tadmin=$('.tadmin');
	if($tadmin.length>0){
		$.each($tadmin, function(index, val) {
			var panelH1 = $(this);
			var $tableResponsive = $("<div>", {class: "table-responsive"});
			// $.each(panelH1,function(index, val) {
			// 	$tableResponsive.before(panelH1);
			// });
			 $panel.append($tableResponsive.append($(this)));
			 //$panelH1.insertBefore($tableResponsive);
		});
		// var $tableResponsive=$('.table-responsive');
		// $.each($tableResponsive, function(index, val) {		
		// 	var panels=$(this).parent().find('h1');
		// 	$(this).before(panels);
		// });
		//$panel.append($tableResponsive.append($tadmin));
		//$tableResponsive.appendTo($tadmin.parent());
	}
	$('.inweb_form .row').addClass('col-lg-12 top10');
	$('.inweb_form  .col-lg-12 dt').addClass('col-lg-2');
	$('.inweb_form  .col-lg-12 dd').addClass('col-lg-10');
	$('input[type="text"] , select , textarea ,.inweb_form .row input[type="password"]').addClass('form-control');
	$('input[type="submit"]').addClass('btn btn-inweb');
	$('input[id="update"],input[id="submit"]').addClass('btn btn-inweb');
	$('.inweb_form .row').removeClass('row');
	$('#Layer1').appendTo('#searchForm .form-inline');
	$('#Layer1').removeAttr('style');
	$('#Layer1 div.form-group').unwrap();
	var $formGroup  = $(" <div>", {class: "form-group"});
	var $formGroup2 = $(" <div>", {class: "form-group",style:"margin-left: 4px;"});
	var $formGroup3 = $(" <div>", {class: "form-group khusus",style:"margin-left: 4px;"});
	var $formGroup4 = $(" <div>", {class: "form-group",style:"margin-left: 4px;"});
	var $inputGroup = $(" <div>", {class: "input-group"});
	var $spanGroup = $(" <span>", {class: "input-group-btn"});
	var $btn_submit = $(" <button>", {class: "btn btn-default",type: "submit",name:"search"});
	var $btn_clear = $(" <button>", {text:"Clear",class: "clearsearch btn btn-default",type: "button",name:"clear"});
	var $icon=$("<i>", {class: "fa fa-search"});
	var $icon2=$("<i>", {class: "fa fa-times"});
	var $form=$('#Layer1 form#searchForm');
	var $select_fieldname=$('#Layer1 form#searchForm select[name="fieldname"]');
	$select_fieldname.addClass('form-control');
	var $select_kriteria=$('#Layer1 form#searchForm select#search_criteria');
	$select_kriteria.addClass('form-control');
	var $input_key=$('#Layer1 form#searchForm input#search_keyword');
	$input_key.addClass('form-control');
	$('#Layer1 form#searchForm input[type="submit"]').remove();
	var $input_clear=$('#Layer1 form#searchForm input[name="clear"]');
	$form.append($formGroup.append($select_fieldname));
	$form.append($formGroup2.append($select_kriteria));
	$form.append(
		$formGroup3.append(
			$inputGroup.append($input_key).append(
				$spanGroup.append(
					$btn_submit.append($icon)))));
	if($input_clear.length>0){
		$input_clear.remove();
		$form.append($formGroup4.append($btn_clear.prepend($icon2)));
	}
	var $type_file=$('input[type="file"]');
		//alert($.inArray( 0, $type_file));
	//var newfile='';
	function create_file(i){
		var newfile = '<div class="row"><div class="col-xs-6"><div class="input-group"><input id="filesname'+i+'" class="form-control" type="text" disabled><span class="input-group-btn"><button class="btn btn-default" type="button" onclick="upload(\''+i+'\');">Browse</button></span></div></div></div>';
		return $('input[type="file"]#files'+i).after(newfile);
	}
	$type_file.each(function(i, el) {
		$(this).attr({'id':'files'+i,'style':'display:none'});
		create_file(i);
	});
	$('#Layer1 form#searchForm input[type="hidden"]').unwrap().unwrap();
	$('#searchForm input[name="clear"]').remove();
	//$('html.tempat-table').find('script').remove();
	var img_logo=$('a.navbar-brand >img');
	img_logo.attr('height', '40px');
	var successmsg=$('.successmsg')
						.addClass('alert alert-success alert-dismissible')
						.attr('role', 'alert');
	var btn_successmsg='<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>';
	successmsg.prepend(btn_successmsg);
	//$('body').append('<script>'+$('.tempat-table > div.themes').first().prev().html()+'</script>');
	$('.tempat-table > div.themes').first().prev().remove();
	//alert($('table.tadmin').html());
	$('table.tadmin').addClass('table table-striped table-bordered table-hover');
	$('#treeboxbox_tree2').removeAttr('style');
	var tess=$('table.tadmin>tbody>tr>td').find('a');
	tess.each(function(index, el) {
		var $this=$(this);
		if($this.text()=="delete"){
			$this.addClass('btn btn-xs btn-danger')
					.prepend('<i class="fa fa-trash"></i>&nbsp');
		}
		if($this.text()=="install"){
			$this.addClass('btn btn-xs btn-success')
					.prepend('<i class="fa fa-check"></i>&nbsp');
		}
		if($this.text()=="uninstall"){
			$this.addClass('btn btn-xs btn-warning')
					.prepend('<i class="fa fa-close"></i>&nbsp');
		}
		if($this.text()=="download"){
			$this.addClass('btn btn-xs btn-primary')
					.prepend('<i class="fa fa-download"></i>&nbsp');
		}
		if($this.text()=="Yes"){
			$this.addClass('btn btn-xs btn-primary');
		}
		if($this.text()=="No"){
			$this.addClass('btn btn-xs btn-danger');
		}
		if($this.text()=="Edit Label"){
			$this.addClass('btn btn-xs btn-inweb').prepend('<i class="fa fa-align-justify"></i>&nbsp');
		}
		if($this.text()=="Edit Field"){
			$this.addClass('btn btn-xs btn-primary').prepend('<i class="fa fa-list"></i>&nbsp');
		}
		if($this.text()=="Edit Layout"){
			$this.addClass('btn btn-xs btn-success').prepend('<i class="fa fa-table"></i>&nbsp');
		}
	});
	var scripttt=$('.tempat-table').find('script');
	scripttt.appendTo('#wrapper');
	$('.inp.order.form-control').attr('style', 'display:inline;width:50px;text-align:center');
	$('a').each(function(index, el) {
	var ini=$(this);
	if(ini.attr('href')=='#reorder'){
		var newAttr='return '+ini.attr('onclick')+';';
		ini.removeAttr('onclick')
			.removeAttr('href')
			.attr({'onclick':newAttr,'href':'javascript:void(0)'});
	}
	});
	//$('.tempat-table > div.themes')..prev().remove();
	//$('.tempat-table > div.themes').addClass('col-lg-3');
	//var ul_errors=$('ul.error');
	initSubmenuIcon();
	admin_cb();
	admin_table();
	admin_tree();
	//select_fieldname.addClass("form-control");
});